import { createRouter, createWebHashHistory } from "vue-router";
import Layout from "../layout/Index.vue";
import NProgress from "nprogress";
import "nprogress/nprogress.css";
import { useMainStore } from "@/store/index.js";
import _ from "lodash";

NProgress.configure({ showSpinner: false });

const router = createRouter({
  history: createWebHashHistory(),
  routes: [
    {
      path: "/login",
      meta: { title: "登录", name: "login" },
      component: () => import("../views/Login.vue"),
    },
    {
      path: "/error",
      meta: { title: "404" },
      component: () => import("../components/NotFound.vue"),
    },
    {
      path: "/:w+",
      redirect: "/error",
    },
    {
      path: "",
      name: "Layout",
      component: Layout,
      // 自定义属性，设置是否必须Authorization
      meta: {
        requireAuth: true,
      },
      children: [
        {
          path: "/emission_monitor_chart",
          meta: { title: "船厂碳排放-集团内部" },
          component: () => import("../views/EmissionMonitorChart.vue"),
        },
        {
          path: "/emission_check_chart",
          meta: { title: "船厂碳排放-政府核查" },
          component: () => import("../views/EmissionCheckChart.vue"),
        },
        {
          path: "/ship_emission_chart",
          meta: { title: "单船研究数据" },
          component: () => import("../views/ShipEmissionChart.vue"),
        },
        {
          path: "/emission",
          meta: { title: "船厂碳排放" },
          component: () => import("../views/Emission.vue"),
        },
        {
          path: "/calibrate",
          meta: { title: "碳标定" },
          component: () => import("../views/Calibrate.vue"),
        },
        {
          path: "/ship_emission",
          meta: { title: "单船工序" },
          component: () => import("../views/ShipEmission.vue"),
        },
        {
          path: "/material",
          meta: { title: "物量信息" },
          component: () => import("../views/Material.vue"),
        },
        {
          path: "/role",
          meta: { title: "角色管理" },
          component: () => import("../views/Role.vue"),
        },
        {
          path: "/user",
          meta: { title: "用户管理" },
          component: () => import("../views/User.vue"),
        },
        {
          path: "/ship",
          meta: { title: "船舶档案" },
          component: () => import("../views/Ship.vue"),
        },
        {
          path: "/order",
          meta: { title: "维修管理" },
          component: () => import("../views/Order.vue"),
        },
        {
          path: "/report",
          meta: { title: "报表中心" },
          component: () => import("../views/Report.vue"),
        },
        {
          path: "/calibrate_chart",
          meta: { title: "标定研究数据" },
          component: () => import("../views/CalibrateChart/index.vue"),
        },
      ],
    },
  ],
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  },
});

router.beforeEach((to, from, next) => {
  NProgress.start();
  if (to.meta.title) {
    document.title = to.meta.title;
  }
  const store = useMainStore();
  // 根据设置的requireAuth客户端简单验证token是否存在
  if (to.matched.some((r) => r.meta.requireAuth)) {
    const token = store.$state._token;
    if (token) {
      next();
    } else {
      next("/login");
    }
  } else {
    next();
  }
});

router.afterEach(() => {
  NProgress.done();
});

export default router;
